import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/utils/widget_utils.dart';
import 'package:ball_lion/module/live/items/gift_effect_view.dart';
import 'package:ball_lion/module/live/items/live_expert_info.dart';
import 'package:ball_lion/module/live/items/live_message_list.dart';
import 'package:ball_lion/module/live/items/vertical_panel.dart';
import 'package:ball_lion/widgets/loading/loading_widget.dart';
import 'package:ball_lion/module/live/live_normal_logic.dart';
import 'package:ball_lion/widgets/card.dart';
import 'package:ball_lion/widgets/share/share_sheet.dart';
import 'package:ball_lion/widgets/player/qs_player_logic.dart';
import 'package:ball_lion/widgets/player/qs_player.dart';
import 'items/live_share_append.dart';

class LiveNormalPage extends StatefulWidget {
  const LiveNormalPage({super.key});

  @override
  State<LiveNormalPage> createState() => _LiveNormalPageState();
}

class _LiveNormalPageState extends State<LiveNormalPage> {
  final logic = Get.put(LiveNormalLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LiveNormalLogic>(builder: (logic) {
      return AnnotatedRegion<SystemUiOverlayStyle>(
        value: const SystemUiOverlayStyle(
            statusBarBrightness: Brightness.light,
            statusBarIconBrightness: Brightness.light),
        child: Scaffold(
          backgroundColor: Colors.black,
          body: LoadingWidget<bool>(
            isLoading: logic.liveDetail == null,
            loading: Container(),
            data: false,
            child: () => Stack(
              alignment: AlignmentDirectional.topCenter,
              children: [
                Stack(
                  children: [
                    QsPlayer(logic: logic.playerLogic),
                    Positioned.fill(child: VerticalPanel(logic: logic))
                  ],
                ),
                if (logic.liveDetail != null &&
                    !logic.waitStart &&
                    logic.liveDetail?.status == 1 &&
                    logic.playerLogic.status != PlayStatus.completion)
                  Positioned.fill(
                    child: LiveMessageList(
                      liveDetail: logic.liveDetail,
                      scene: MessageScene.normal,
                    ),
                  ),
                const GiftEffect(),
                Positioned(top: 0, left: 0, right: 0, child: topBar()),
              ],
            ),
          ),
        ),
      );
    });
  }

  Widget topBar() {
    return SafeArea(
        child: Container(
            padding: const EdgeInsets.symmetric(vertical: 4),
            child: Row(children: [
              // if(!logic.waitStart)
              LiveExpertInfo.normal(
                liveDetail: logic.liveDetail,
                onFocusChanged: (focus) {
                  logic.liveDetail?.isFocus = focus ? 1 : 0;
                  logic.getData();
                },
              ),
              const Spacer(),
              Block(
                onTap: () async {
                  Utils.onEvent('spzb', params: {'spzb': 2});
                  Get.bottomSheet(
                      ShareSheet(
                          fromLive: true,
                          append: LiveShareAppend(liveId: logic.liveId ?? 0)),
                      settings: RouteSettings(
                          arguments: ShareEntity(
                        title: "【直播】这个专家超厉害，快来围观TA的直播",
                        content: "免费比赛分析，尽在球狮体育App",
                      )));
                },
                cardStyle: const CardStyle(
                  color: Colours.transparent,
                  padding: EdgeInsets.symmetric(horizontal: 8, vertical: 10),
                ),
                child: Image.asset(
                  Utils.getImgPath("live_share.png"),
                  color: Colours.white,
                  width: 24,
                  height: 24,
                ),
              ),
              Padding(
                padding:
                    const EdgeInsets.symmetric(horizontal: 8, vertical: 10),
                child: Image.asset(Utils.getImgPath("close.png"),
                    width: 24, color: Colours.white),
              ).tap(logic.exit),
              Container(width: 16),
            ])));
  }
}
